package com.example.hello.mapper;

import com.example.hello.pojo.SMSCode;
import org.apache.ibatis.annotations.*;

/**
 * MyBatis 注解方式操作短信验证码记录表
 */
@Mapper
public interface SMSCodeMapper {

    /**
     * 根据手机号获取最近一条验证码记录
     */
    @Select("SELECT * FROM sms_code WHERE phone = #{phone} ORDER BY send_time DESC LIMIT 1")
    SMSCode getLatestCodeByPhone(@Param("phone") String phone);

    /**
     * 插入验证码记录
     */
    @Insert("INSERT INTO sms_code (phone, code, send_time, expire_time, status) " +
            "VALUES (#{phone}, #{code}, #{sendTime}, #{expireTime}, #{status})")
    int insertSMSCode(SMSCode smsCode);

    /**
     * 更新验证码状态为已使用
     */
    @Update("UPDATE sms_code SET status = 1 WHERE id = #{id}")
    int updateStatus(@Param("id") Long id);
}